package com.frxuexi.park2304.my

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.NewsBeanItem
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.entity.StatusBean
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.ActivityMyApproveBinding
import com.frxuexi.visit.adapter.StatusAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/MyApproveActivity")
@AndroidEntryPoint
class MyApproveActivity : BaseActivity<ActivityMyApproveBinding,MyIntent,MyViewModel>() {
    override val binding: ActivityMyApproveBinding by lazy { ActivityMyApproveBinding.inflate(layoutInflater) }
    override val vm: MyViewModel by viewModels<MyViewModel>()
    val statusAdapter by lazy { StatusAdapter() }
    val myApproveAdapter by lazy { MyApproveAdapter() }
    val parkList= mutableListOf<ParkBeanItem>()
    val visitList= mutableListOf<VisitBeanItem>()
    val newsList= mutableListOf<NewsBeanItem>()

    var position=0

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.PARKSTATUS->{
                                val items = it.data as Collection<ParkBeanItem>
                                items.forEach { it.itemType=0 }
                                parkList.addAll(items)
                                myApproveAdapter.addData(items)
                                myApproveAdapter.notifyDataSetChanged()
                            }
                            RequestType.MYVISIT->{
                                val items = it.data as Collection<VisitBeanItem>
                                items.forEach { it.itemType=1 }
                                visitList.addAll(items)
                                myApproveAdapter.addData(items)
                                myApproveAdapter.notifyDataSetChanged()
                            }
                            RequestType.NEWS->{
                                val items = it.data as Collection<NewsBeanItem>
                                val list=items.filter { it.published }
                                list.forEach { it.itemType=2 }
                                newsList.addAll(list)
                                myApproveAdapter.addData(items)
                                myApproveAdapter.notifyDataSetChanged()
                            }
                            RequestType.UPDATE->{
                                ToastUtils.showLong("操作完成")
                            }

                            else -> {}
                        }
                    }
                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        val statusBeans = mutableListOf<StatusBean>(
            StatusBean("全部",true),
            StatusBean("车位审核",false),
            StatusBean("来访审核",false),
            StatusBean("新闻审核",false)
        )
        statusAdapter.addData(statusBeans)
        binding.appRv.apply {
            adapter=statusAdapter
            layoutManager=LinearLayoutManager(this@MyApproveActivity,RecyclerView.HORIZONTAL,false)
        }

        statusAdapter.setOnItemClickListener { adapter, view, position ->
            statusAdapter.data.forEach {
                it.isCheck=false
            }
            statusAdapter.data.get(position).isCheck=true
            statusAdapter.notifyDataSetChanged()

            this.position=position
            myApproveAdapter.apply {
                data.clear()
                if(position==0){
                    addData(parkList)
                    addData(visitList)
                    addData(newsList)
                }else if(position==1){
                    addData(parkList)
                }else if(position==2){
                    addData(visitList)
                }else{
                    addData(newsList)
                }
                notifyDataSetChanged()
            }
        }

        binding.rvApprove.apply {
            adapter=myApproveAdapter
            layoutManager=LinearLayoutManager(this@MyApproveActivity)
        }

        myApproveAdapter.addChildClickViewIds(com.frxuexi.park.R.id.btn_ok,com.frxuexi.park.R.id.btn_no,com.frxuexi.visit.R.id.vistit_ok,com.frxuexi.visit.R.id.visit_no,R.id.new_detail)
        myApproveAdapter.setOnItemChildClickListener { adapter, view, position ->
            val bean = myApproveAdapter.data.get(position)
            when(view.id){
                com.frxuexi.park.R.id.btn_ok->{
                    val item = bean as ParkBeanItem
                    vm.sendIntent(MyIntent.updatePark(item.id!!,"维护中"))
                    bean.spaceStatus="维护中"
                }
                com.frxuexi.park.R.id.btn_no->{
                    val item = bean as ParkBeanItem
                    vm.sendIntent(MyIntent.updatePark(item.id!!,"已占用"))
                    bean.spaceStatus="已占用"
                }
                com.frxuexi.visit.R.id.vistit_ok->{
                    val item = bean as VisitBeanItem
                    vm.sendIntent(MyIntent.approveVisit(item.id!!))
                    bean.visitStatus="已通过"
                }
                com.frxuexi.visit.R.id.visit_no->{
                    val item = bean as VisitBeanItem
                    vm.sendIntent(MyIntent.rejectVisit(item.id!!))
                    bean.visitStatus="驳回"
                }
                R.id.new_detail->{
                    val item = bean as NewsBeanItem
                    vm.sendIntent(MyIntent.revokeNews(item.id!!))
                    bean.published=false
                    myApproveAdapter.notifyItemRemoved(position)
                    return@setOnItemChildClickListener
                }
            }
            myApproveAdapter.notifyItemChanged(position)
        }

        vm.sendIntent(MyIntent.getParksByType("标准车位"))
        vm.sendIntent(MyIntent.getVisits)
        vm.sendIntent(MyIntent.getNews)

        binding.appSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                myApproveAdapter.apply {
                    data.clear()
                    if(position==0){
                        addData(parkList.filter { it.remarks==query!! })
                        addData(visitList.filter { it.purpose==query!! })
                        addData(newsList.filter { it.content==query!! })
                    }else if(position==1){
                        addData(parkList.filter { it.remarks==query!! })
                    }else if(position==2){
                        addData(visitList.filter { it.purpose==query!! })
                    }else{
                        addData(newsList.filter { it.content==query!! })
                    }
                    notifyDataSetChanged()
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if(newText==""){
                    myApproveAdapter.apply {
                        data.clear()
                        if(position==0){
                            addData(parkList)
                            addData(visitList)
                            addData(newsList)
                        }else if(position==1){
                            addData(parkList)
                        }else if(position==2){
                            addData(visitList)
                        }else{
                            addData(newsList)
                        }
                        notifyDataSetChanged()
                    }
                }
                return true
            }
        })
    }



}